<template>
  <div class="page-search">
    <HyForm v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <div class="header-word">这是标题</div>
      </template>
      <template #foot>
        <div class="button-box">
          <el-button type="primary" @click="handleResetClick">
            <el-icon>
              <RefreshLeft />
            </el-icon>
            重置
          </el-button>
          <el-button type="primary" @click="handleQueryClick">
            <el-icon>
              <Search />
            </el-icon>
            搜索
          </el-button>
        </div>
      </template>
    </HyForm>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";

import HyForm from "@/base-ui/form";
import { RefreshLeft, Search } from "@element-plus/icons-vue";

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      required: true,
    },
  },
  components: {
    HyForm,
    RefreshLeft,
    Search,
  },
  emits: ["resetBtnClick", "queryBtnClick"],
  setup(props, { emit }) {
    //动态获取配置文件里面的formdata
    const formItems = props.searchFormConfig?.formItems ?? [];
    const formOriginData: any = {};
    // formItems.forEach((item: any) => {
    //   formOriginData[item.field] = "";
    // });
    for (const item of formItems) {
      formOriginData[item.field] = "";
    }
    const formData = ref(formOriginData);

    // 用户点击重置
    const handleResetClick = () => {
      for (const key in formOriginData) {
        formData.value[`${key}`] = formOriginData[key];
      }
      emit("resetBtnClick");
    };
    // 用户点击搜索
    const handleQueryClick = () => {
      emit("queryBtnClick", formData.value);
    };

    return {
      formData,
      handleResetClick,
      handleQueryClick,
    };
  },
});
</script>
<style scoped lang="less">
.header-word {
  text-align: center;
  font-size: 30px;
}
.button-box {
  text-align: right;
  padding: 0 50px 20px 0;
  .el-icon {
    margin-right: 10px;
  }
}
</style>
